import './config/env';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // 添加全局验证管道
  app.useGlobalPipes(new ValidationPipe());

  // Swagger配置
  const config = new DocumentBuilder()
    .setTitle('Pocket Meter API')
    .setDescription('Pocket Meter 应用程序的API文档')
    .setVersion('1.0')
    .addBearerAuth()
    .build();

  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api', app, document, {
    customSiteTitle: 'Pocket Meter API 文档',
    customfavIcon: 'https://fastapi.tiangolo.com/img/favicon.png',
    customJs: [
      'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui-themes/3.0.0/themes/3.x/theme-feeling-blue.css',
      'https://gcore.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui-bundle.js',
    ],
    customCssUrl: [
      'https://gcore.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui.css',
    ],
    swaggerOptions: {
      persistAuthorization: true,
      docExpansion: 'none',
      filter: true,
      showRequestDuration: true,
      syntaxHighlight: {
        theme: 'monokai',
      },
      defaultModelsExpandDepth: 2,
      defaultModelExpandDepth: 2,
      displayOperationId: false,
      displayRequestDuration: true,
      theme: 'feeling-blue',
      languageUrl:
        'https://gcore.jsdelivr.net/npm/swagger-ui-dist@5.9.0/lang/zh-cn.js',
    },
  });

  await app.listen(process.env.PORT ?? 3000);
}

bootstrap();
